package com.aso114.project.mvp.activity.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.aso114.project.R;
import com.aso114.project.adapter.CollectAdapter;
import com.aso114.project.adapter.Error_item_adapter;
import com.aso114.project.base.BaseFragment;
import com.aso114.project.bean.QuestionBean;
import com.aso114.project.bean.QuestionEvent;
import com.aso114.project.exam.ExpositionTestActivity;
import com.aso114.project.exam.QuestionTestActivity;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Constant;
import com.aso114.project.util.Helper;
import com.aso114.project.util.ToastUtils;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 申论的错题练习
 */
public class ErrorExerciseRightFragment extends BaseFragment  implements SwipeRefreshLayout.OnRefreshListener, SwipeMenuRecyclerView.LoadMoreListener{

    @BindView(R.id.my_collection_ly)
    LinearLayout myCollectionLy;
    @BindView(R.id.my_collection_listView)
    SwipeMenuRecyclerView myCollectionListView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;

    Unbinder unbinder;
    Dialog dialog;
    QuestionBean qdata;

    ArrayList<QuestionBean.QuestionBaseListBean.questionListBean> data = new ArrayList<>();
    Error_item_adapter collectAdapter;
    //刷新
    private boolean isRefresh = false;

    private int pageNo = 1;
    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_errorexercise;

    }

    @Override
    protected void initView() {


        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //设置布局管理器
        myCollectionListView.setLayoutManager(layoutManager);
        collectAdapter = new Error_item_adapter(getActivity());
        collectAdapter.setData(data);
        myCollectionListView.setAdapter(collectAdapter);
        // 刷新
        swipeRefreshLayout.setOnRefreshListener(this);
        // 加载
        myCollectionListView.useDefaultLoadMore();
        myCollectionListView.setLoadMoreListener(this);
        isRefresh = true;


        getData();
//        collectAdapter.setOnItemClickListener(this);
        // 设置item及item中控件的点击事件
        collectAdapter.setOnItemClickListener(MyItemClickListener);


    }


    @Override
    public void onRefresh() {
        // 刷新
        // 下拉刷新
        pageNo = 1;
        isRefresh = true;
        getData();
    }

    @Override
    public void onLoadMore() {
        // 加载
        // 上拉加载
        pageNo++;
        isRefresh = false;
        getData();
    }


    private void getData() {

        if (isRefresh) {
            swipeRefreshLayout.setRefreshing(true);
        }


        CommentModel.getInstant().geterror_exercise(pageNo,Constant.essay, new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                Helper.gotoLogin(isSuccess);//token失效去重新登陆
                qdata = (QuestionBean) o;

                if (swipeRefreshLayout == null) {
                    return;
                }
                if (isRefresh) {
                    pageNo = 1;
                    swipeRefreshLayout.setRefreshing(false);
                    data.clear();
                }
                if (qdata == null || qdata.getQuestionBaseList().get(0).getQuestionList().size() == 0) {
                    if(pageNo==1){
                        myCollectionLy.setVisibility(View.VISIBLE);
                        swipeRefreshLayout.setVisibility(View.GONE);
                    }else{


                        myCollectionListView.loadMoreFinish(true, true);
                        collectAdapter.notifyDataSetChanged();
                        ToastUtils.showToast(getActivity(),"已加载全部数据");
                    }

                } else {

                    myCollectionLy.setVisibility(View.GONE);
                    swipeRefreshLayout.setVisibility(View.VISIBLE);
                    data.addAll(qdata.getQuestionBaseList().get(0).getQuestionList());
                    collectAdapter.notifyDataSetChanged();
                    myCollectionListView.loadMoreFinish(false, true);

                }

            }

            @Override
            public void onFailure(String fail) {
                swipeRefreshLayout.setRefreshing(false);
                myCollectionListView.loadMoreFinish(true, true);
                Helper.showToast(fail);
            }
        });
    }


    /**
     * item＋item里的控件点击监听事件
     */
    private Error_item_adapter.OnItemClickListener MyItemClickListener = new Error_item_adapter.OnItemClickListener() {

        @Override
        public void onItemClick(View v, Error_item_adapter.ViewName viewName,final int position) {
            //viewName可区分item及item内部控件
            switch (v.getId()){
                case R.id.my_collect_clear:
//                    Toast.makeText(getActivity(),"你点击了同意按钮"+(position+1),Toast.LENGTH_SHORT).show();
                    CommentModel.getInstant().del_error_topic(data.get(position).getQuestionId(),data.get(position).getPaperId(),Constant.essay, new CallBack() {
                        @Override
                        public void onSuccess(Object o, boolean isSuccess, String msg) {
                            Helper.gotoLogin(isSuccess);//token失效去重新登陆
                            data.remove(position);
                            collectAdapter.notifyDataSetChanged();

                            if (data.size() == 0) {
                                myCollectionLy.setVisibility(View.VISIBLE);
                                swipeRefreshLayout.setVisibility(View.GONE);

                            } else {
                                myCollectionLy.setVisibility(View.GONE);
                                swipeRefreshLayout.setVisibility(View.VISIBLE);

                            }

                        }

                        @Override
                        public void onFailure(String fail) {
                            Helper.showToast(fail);
                        }
                    });

                    break;
                case R.id.my_collect_item_ly:
                    EventBus.getDefault().postSticky(qdata);
                    Intent intent = new Intent(getActivity(), ExpositionTestActivity.class);
                    intent.putExtra("isAnalysis",true);
                    intent.putExtra("position",position);
                    startActivity(intent);
                    break;
            }
        }

        @Override
        public void onItemLongClick(View v) {

        }
    };


    @Override
    protected void initData() {

    }

    @Override
    protected void addOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
